package com.teamwings.modules.cate.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teamwings.common.constant.FormConstant;
import com.teamwings.modules.cate.entity.FormTableCate;
import com.teamwings.modules.cate.service.IFormTableCateService;
import com.teamwings.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-05-05
 */
@RestController
@Api(tags = "表单分类管理")
@RequestMapping("/form/cate")
public class FormTableCateController {

    @Autowired
    private IFormTableCateService formTableCateService;

    /**
     * 分页查询
     */
    @Cacheable(value = FormConstant.FORM_CATE_PAGING, key="#page+'_'+#pageSize")
    @ApiOperation(value = "获取cate分页", notes = "获取cate分页")
    @GetMapping(value = "/list")
    public Result<?> list(
            @RequestParam(value = "page", defaultValue = "1") Long page,
            @RequestParam(value = "pageSize", defaultValue = "20") Long pageSize,
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "cate_id", required = false) String cateId
    ) {
        Page<FormTableCate> catePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<FormTableCate> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(cateId)) {
            queryWrapper.eq(FormTableCate::getId, cateId);
        }
        if (StringUtils.isNotEmpty(key)) {
            queryWrapper.eq(FormTableCate::getName, key);
        }
        IPage<FormTableCate> list = formTableCateService.page(catePage, queryWrapper);
        return Result.OK(list);
    }

    /**
     * 添加表单分类
     */
    @CacheEvict(value = {FormConstant.FORM_CATE_PAGING, FormConstant.FORM_CATE_TREE_SYNC}, key = "'*'")
    @ApiOperation(value = "添加cate数据", notes = "添加cate数据")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody FormTableCate cate) {
        if (cate.getPid() == null) {
            cate.setPid("0");
            cate.setHasChild(0);
        }
        formTableCateService.saveCate(cate);
        return Result.OK("添加成功！");
    }

    /**
     * 更新表单分类
     */
    @CacheEvict(value = {FormConstant.FORM_CATE_PAGING, FormConstant.FORM_CATE_TREE_SYNC}, key = "'*'")
    @ApiOperation(value = "更新cate数据", notes = "更新cate数据")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody FormTableCate cate) {
        formTableCateService.updateById(cate);
        return Result.OK("添加成功！");
    }

    /**
     * 根据ID删除表单分类
     */
    @CacheEvict(value = {FormConstant.FORM_CATE_PAGING, FormConstant.FORM_CATE_TREE_SYNC}, key = "'*'")
    @ApiOperation(value = "删除cate数据", notes = "删除cate数据")
    @DeleteMapping(value = "/removeById")
    public Result<?> removeById(@RequestParam(value = "id", required = false) String id) {
        formTableCateService.removeByIdMain(id);
        return Result.OK("添加成功！");
    }

    /**
     * 删除多个表单分类
     */
    @CacheEvict(value = {FormConstant.FORM_CATE_PAGING, FormConstant.FORM_CATE_TREE_SYNC}, key = "'*'")
    @ApiOperation(value = "删除cate列表数据", notes = "删除cate列表数据")
    @GetMapping(value = "/removeByIdList")
    public Result<?> removeByIdList(@RequestParam(value = "ids", required = false) List<String> ids) {
        int i = formTableCateService.removeByIdListMain(ids);
        return Result.OK("删除完成！共删除" + i + "条。");
    }

    /**
     * 分类树：一次性加载整颗树
     */
    @Cacheable(value = FormConstant.FORM_CATE_PAGING, key="#root.method.name")
    @ApiOperation(value = "分类树(一次性加载整颗树)", notes = "分类树(一次性加载整颗树)")
    @GetMapping(value = "/tree")
    public Result<?> tree() {
        List<FormTableCate> tree = formTableCateService.getTree();
        return Result.OK("获取成功！", tree);
    }

    /**
     * 懒加载，根据pid寻找对应的栏目
     */
    @Cacheable(value = FormConstant.FORM_CATE_TREE_SYNC, key = "'_' + #pid")
    @ApiOperation(value = "懒加载，根据pid寻找对应的栏目", notes = "懒加载，根据pid寻找对应的栏目")
    @GetMapping(value = "/queryCateTreeSync")
    public Result<?> queryCateTreeSync(@RequestParam(value = "pid", required = false) String pid) {
        List<FormTableCate> list = formTableCateService.queryCateTreeSync(pid);
        return Result.OK("获取成功！", list);
    }


    @ApiOperation(value = "根据name寻找对应的分类", notes = "根据name寻找对应的分类")
    @GetMapping(value = "/getCateByName")
    public Result<?> getCateByName(@RequestParam(value = "name", required = false) String name) {
        List<FormTableCate> list = formTableCateService.lambdaQuery().like(FormTableCate::getName, name).list();
        return Result.OK("获取成功！", list);
    }
}
